<!--
 * @Author: wankong
 * @Desc: 
 * @Date: 2021-10-10 20:13:36
 * @LastEditTime: 2021-10-18 22:02:17
-->
<template>
  <div class="Links">
    <el-row>
      <el-col>
        <el-button type="primary" @click="addshow()">添加</el-button>
        <el-link> </el-link>
      </el-col>
    </el-row>
    <el-table :data="linkList">
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="url" label="链接"></el-table-column>
      <el-table-column prop="linkdesc" label="描述"></el-table-column>
      <el-table-column prop="logo" label="logo"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="warning" @click="showEdit(scope.row)" icon="editor"
            >编辑</el-button
          >
          <el-button type="danger" @click="delLink(scope.row)" icon="delete"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="填写信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="linkrefs"
        :rules="editRule"
        :model="link"
        label-width="100px"
      >
        <el-form-item label="友链名称:" prop="name">
          <el-input v-model="link.name" placeholder="在这里输入"></el-input>
        </el-form-item>
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="link.url" placeholder="描述信息"></el-input>
        </el-form-item>
        <el-form-item label="描述信息：" prop="linkdesc">
          <el-input v-model="link.linkdesc" placeholder="描述信息"></el-input>
        </el-form-item>
        <el-form-item label="logo地址：">
          <el-input v-model="link.logo" placeholder="图片的绝对地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeEdit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      linkList: [],
      link: {},
      editRule: {
        name: {
          required: true,
          message: "名字必须填",
          trigger: "blur",
        },
        linkdesc: {
          required: true,
          message: "描述信息必填",
          trigger: "blur",
        },
        url: {
          required: true,
          message: "友链地址要填",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    async getLinkList() {
      const res = await this.$http.get(`${this.adminPre}/link`);
      this.linkList = res.data;
    },
    // 弹窗关闭
    handleClose() {
      this.link = {};
      this.dialogVisible = false;
    },
    showEdit(row) {
      this.link = row;
      this.dialogVisible = true;
    },
    async delLink(row) {
      this.$confirm("此操作将永久删除该友链, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.delete(
            `${this.adminPre}/link/${row.id}`
          );
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getLinkList();
          } else {
            this.$message({
              type: "info",
              message: "删除失败!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    changeEdit() {
      this.$refs["linkrefs"].validate(async (valid) => {
        if (!valid) return;
        const res = await this.$http.post(`${this.adminPre}/link`, this.link);
        if (res.code == 200) {
          this.$message.success("成功！");
          this.dialogVisible = false;
          this.link = {};
          this.getLinkList();
        } else {
          this.$message.error("失败");
        }
      });
    },
    addshow() {
      this.dialogVisible = true;
    },
  },
  created() {
    this.getLinkList();
  },
};
</script>

<style>
</style>